package com.mlz.mlzmall.ui.activity.act.personal;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.GetMyPromotionResponse;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.GetMyPromotionResponse.DataBean;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.GetMyPromotionResponse.DataBean.ListBean;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyActivityAdapter;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyActivityRequestUtils;
import com.mlz.mlzmall.ui.activity.act.personal.myActivityPack.ActionActivity;
import com.mlz.mlzmall.ui.activity.act.personal.myActivityPack.ShareActivity;
import com.mlz.mlzmall.ui.dialog.ReuestDialog;
import com.mlz.mlzmall.ui.model.StringTools;
import com.mlz.mlzmall.ui.utils.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 我的活动
 * Created by 安帅 on 2016/5/23 16:13.
 */
public class MyNewActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private static final String TGA = "MyNewActivity";
    private List<ListBean> list;
    private MyActivityAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        //initData();
        assignViews();
        setViewContent();
        //   setViewLinstener();
        LogUtils.d(sessionId);
        getPromotion();
    }

    private TopTitleView deviceManagerTitle;
    private FrameLayout viewBlankFraWhole;
    private ImageView viewBlankImgFail;
    private TextView viewBlankTxtFail;
    private ListView deviceManagerLvDevicelist;

    private void assignViews() {
        deviceManagerTitle = (TopTitleView) findViewById(R.id.device_manager_title);
        viewBlankFraWhole = (FrameLayout) findViewById(R.id.view_blank_fra_whole);
        viewBlankImgFail = (ImageView) findViewById(R.id.view_blank_img_fail);
        viewBlankTxtFail = (TextView) findViewById(R.id.view_blank_txt_fail);
        deviceManagerLvDevicelist = (ListView) findViewById(R.id.device_manager_lv_devicelist);
    }

    private void setViewContent() {
        deviceManagerTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        deviceManagerTitle.setTitleButtonContent("我的活动", null, null);
    }

    private void setViewLinstener() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        } else {
            adapter = new MyActivityAdapter(MyNewActivity.this, list, R.layout.list_item_activity);
            deviceManagerLvDevicelist.setAdapter(adapter);
        }
        deviceManagerTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
                MyNewActivity.this.overridePendingTransition(R.anim.right_in, R.anim.right_out);

            }
        });
        deviceManagerLvDevicelist.setOnItemClickListener(this);

    }

    private void initData() {
        list = new ArrayList<>();
        Log.i(TGA, "initData" + list);
        adapter = new MyActivityAdapter(mContext, list, R.layout.list_item_activity);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //  getPromotion();
    }

    private void getPromotion() {
        ReuestDialog.showProgressDialog(MyNewActivity.this);//显示加载框
        executeRequest(MyActivityRequestUtils.getMLZActivityList(mContext, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                ReuestDialog.dissmissProgressDialog();
                Gson gson = new Gson();
                GetMyPromotionResponse pr = gson.fromJson(s, GetMyPromotionResponse.class);
                DataBean db = pr.getData();
                list = db.getList();
                setViewLinstener();
//                LogUtils.d(list.get(0).getName());
                if (pr.getData().getTotal() == 0) {
                    viewBlankFraWhole.setVisibility(View.VISIBLE);
                    viewBlankImgFail.setVisibility(View.VISIBLE);
                    viewBlankTxtFail.setText("目前还没有参加任何活动");
                } else {
                    Log.i(TGA, "数据" + list.toString());
                    viewBlankFraWhole.setVisibility(View.GONE);
                    viewBlankImgFail.setVisibility(View.GONE);
//                    setViewLinstener();
                }
            }
        }));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (list.get(position).getType() == 1) {
            Intent intent = new Intent(MyNewActivity.this, ActionActivity.class);
            intent.putExtra(ActionActivity.MY_ACTIVITY_IMAGEURL, list.get(position).getImage());
            intent.putExtra(ActionActivity.MY_ACTIVITY_TYPE, list.get(position).getType() + "");
            intent.putExtra(ActionActivity.MY_ACTIVITY_PROMOTIONID, list.get(position).getPromotionId() + "");
            intent.putExtra(ActionActivity.MY_ACTIVITY_STATE, list.get(position).getState());
            StringTools.shareid = 8;
            startActivity(intent);


        } else if (list.get(position).getType() == 2) {
            Intent intent = new Intent(MyNewActivity.this, ShareActivity.class);
            intent.putExtra("State", list.get(position).getState());
            intent.putExtra("promotionId", list.get(position).getPromotionId()+"");
            StringTools.shareid = 5;
            startActivity(intent);
        }
    }
}
